# -*- coding: utf-8 -*-
# author: sixmillions
# date: 2022/04/20

"""
解决重名问题
pip3 install pandas
"""

import pandas as pd
from wxpy import *

# 常量
# 群名称，可自定义
GROUP_NAME = 'wxpy测试群'
# 成员（成员在群内名称）
all_member = ['张三', '张三', '李四', '王五', '陈小六']
all_series = pd.value_counts(all_member)
# 关键字（自定义）
KEY_WORD = '接龙'
TIPS_NUM = 20


def login():
    print("--login--")
    # 登陆微信(用自动打开登录码，适用于windows)
    # bot = Bot(cache_path=False)  # True代表本地缓存，不用每次重新扫码
    # linux登录（将码输出到控制台）
    bot = Bot(console_qr=2, cache_path=False)
    return bot


def listen_msg(bot):
    print("--listen--")
    dest_group = bot.groups(update=True).search(GROUP_NAME)[0]
    print("群名称：", dest_group)

    @bot.register(dest_group, msg_types=TEXT)
    def group(msg):
        # print(msg.text)  # 输出群消息
        count_map = {}
        if KEY_WORD in msg.text:  # 是否包含接龙
            print("是接龙消息")
            msg_con = msg.text
            for member in all_member:
                count_map[member] = msg_con.count(member)
            count_series = pd.Series(count_map)
            finished_num = count_series.sum()  # 接龙人数
            print('已登记人数：%s人' % finished_num)
            if finished_num == len(all_member):
                # print("已经完成接龙，共%s人" % finished_num)
                # 发送微信消息
                msg.sender.send("【统计】今日已经完成接龙，共%s人" % finished_num)
            elif TIPS_NUM < finished_num < len(all_member):  # 申报超过20人，显示未申报人员
                un_finished_members = []  # 还没接龙的人员，初始为空
                res_series = all_series - count_series
                res_dict = res_series[res_series > 0].to_dict()
                for k, v in res_dict.items():
                    for i in range(0, v):
                        un_finished_members.append(k)
                # print("当前接龙：%s人。未上报人员：%s。请及时接龙" % (finished_num, ", ".join(un_finished_members)))
                # 发送微信消息
                msg.sender.send("【统计】当前接龙：%s人。未上报人员：%s。请及时接龙" % (finished_num, ", ".join(un_finished_members)))
        else:
            print("不是接龙消息")
            print(msg.text)

    bot.join()  # 开启线程


if __name__ == "__main__":
    bot = login()
    listen_msg(bot)
